package com.zl.smartmall.library;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.loopj.android.http.RequestParams;
import com.zl.smartmall.library.account.IAuth;
import com.zl.smartmall.library.concurrent.ThreadExecutor;
import com.zl.smartmall.library.im.IM;
import com.zl.smartmall.library.listener.ActivitiesListener;
import com.zl.smartmall.library.listener.AdvertiseListener;
import com.zl.smartmall.library.listener.BaseListener;
import com.zl.smartmall.library.listener.BrandedProductsListener;
import com.zl.smartmall.library.listener.CancelOrderListener;
import com.zl.smartmall.library.listener.ConfirmTheGoodsListener;
import com.zl.smartmall.library.listener.DeleteOrderListener;
import com.zl.smartmall.library.listener.EquimentConfirmListener;
import com.zl.smartmall.library.listener.GetAssortFirstlevelListener;
import com.zl.smartmall.library.listener.GetAssortProductslListener;
import com.zl.smartmall.library.listener.GetInvoiceTypeListener;
import com.zl.smartmall.library.listener.GetMaintenaceListListener;
import com.zl.smartmall.library.listener.GetOrdersListener;
import com.zl.smartmall.library.listener.GetParticipationListener;
import com.zl.smartmall.library.listener.MaintenancesProjectListener;
import com.zl.smartmall.library.listener.MyAddressListener;
import com.zl.smartmall.library.listener.OrderConfirmationListener;
import com.zl.smartmall.library.listener.OrderDetailListener;
import com.zl.smartmall.library.listener.ProductDetailListener;
import com.zl.smartmall.library.listener.ProductEvaluationDetailListener;
import com.zl.smartmall.library.listener.RequestCode;
import com.zl.smartmall.library.listener.ShopsIntroduceListener;
import com.zl.smartmall.library.listener.SubmitApplyExchangeListener;
import com.zl.smartmall.library.listener.SubmitMaintenacesListener;
import com.zl.smartmall.library.listener.SubmitOrderListener;
import com.zl.smartmall.library.listener.UploadApplyExchangePictureListener;
import com.zl.smartmall.library.listener.UploadEquimentPictureListener;
import com.zl.smartmall.library.listener.UploadEvaluationContentListener;
import com.zl.smartmall.library.listener.UploadEvaluationPictureListener;
import com.zl.smartmall.library.po.AdvertInfo;
import com.zl.smartmall.library.po.BrandedProductsInfo;
import com.zl.smartmall.library.po.MaintenacesInfos;
import com.zl.smartmall.library.po.OrdersInfo;
import com.zl.smartmall.library.po.ParticipationInfo;
import com.zl.smartmall.library.po.ProductEvaluationInfo;
import com.zl.smartmall.library.po.SubmitOrderInfo;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;
import com.zl.smartmall.library.util.PreferenceModel;
import com.zl.smartmall.library.util.SingtonAsyncHttpClient;
import com.zl.smartmall.library.util.Tools;
import com.zl.smartmall.library.vo.AdvertInfoDao;
import com.zl.smartmall.library.vo.BrandedProductsInfoDao;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;

/**
 * 商城相关操作接口
 * @author 陈希然
 *
 */
public class ISmartMall {

	private static final String TAG = ISmartMall.class.getSimpleName();
	private static ISmartMall mInstance;
	private static Lock mLock;

	private Context mContext;
	private Handler mHandler;

	static{
		mLock = new ReentrantLock();
	}

	private ISmartMall(Context context) {
		IAuth.init(context);
		IM.init(context);
		IPay.init(context);
		IShoppingCart.init(context);
		IAddress.init(context);
		IGZAddress.init(context);
		INotice.init(context);
		PreferenceModel.init(context);
		mContext = context;
		mHandler = new ISmartMallHandler(Looper.myLooper());
	}

	public static ISmartMall getInstance() {
		return mInstance;
	}

	public static void init(Context context) {
		if(mInstance == null){
			//        	mLock = new ReentrantLock();
			mLock.lock();
			//    		synchronized (mLock) {
			try{
				if(mInstance == null){
					mInstance = new ISmartMall(context);
				}
			}finally{
				mLock.unlock();
			}
			//			}
		}
	}

	/**
	 * 获取某一品牌下的商品列表
	 * @param fromCache 是否从本地加载; 设置true表示从本地加载, 不需要联网
	 * @param activitiesId 活动id
	 * @param page 页码; 起始值为1; 如果从本地加载则此值将被忽略
	 * @param brandedProductsListener 回调监听器
	 */
	public void getBrandedProducts(boolean fromCache, final int activitiesId, int page, final BrandedProductsListener brandedProductsListener) {
		if(fromCache) {
			ThreadExecutor.getInstance().execute(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					List<Object> value = new ArrayList<Object>();
					AdvertInfoDao advertInfoDao = new AdvertInfoDao(mContext);
					BrandedProductsInfoDao brandedProductsInfoDao = new BrandedProductsInfoDao(mContext);
					value.add(advertInfoDao.query(Constants.ADVERT_AFFILIATION_BRANDED_PRODUCTS));
					value.add(brandedProductsInfoDao.query(activitiesId));

					MessageData msgData = new MessageData();
					msgData.listener = brandedProductsListener;
					msgData.value = value;
					Message msg = Message.obtain(mHandler, Constants.MSG_LOAD_BRANDED_PRODUCTS_FINISH, msgData);
					mHandler.sendMessage(msg);
				}
			});

		}else if(!Tools.hasInternetConnection(mContext)) {
			brandedProductsListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("act_id", activitiesId);
			params.put("page", page);
			brandedProductsListener.activitiesId = activitiesId;
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_BRANDED_PRODUCTS, params, brandedProductsListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_BRANDED_PRODUCTS + "?" + params);
		}
	}


	/**
	 * 获取商城首页
	 * @param communityId 小区id
	 * @param page 页码; 起始值为1
	 * @param activitiesListener 回调监听器
	 */
	public void getHomePage(int communityId,int houseid, int page, ActivitiesListener activitiesListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			activitiesListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("page", page);
			params.put("cmid", communityId);
			params.put("houseid", houseid);
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_MALL_ACTIVITIES, params, activitiesListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_MALL_ACTIVITIES + "?" + params);
		}
	}

	/**
	 * 获取商品详情数据
	 * @param shopId 商铺id
	 * @param productId 商品id
	 * @param productDetailListener 回调监听器
	 */
	public void getProductDetail(int shopId, int productId, ProductDetailListener productDetailListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			productDetailListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("shop_id", shopId);
			params.put("product_id", productId);
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_PRODUCT_DETAIL, params, productDetailListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_PRODUCT_DETAIL + "?" + params);
		}
	}

	/**
	 * 获取某一商品的评价详情
	 * @param productId 商品id
	 * @param page 页码; 取值为{@link ProductEvaluationInfo#getCommentId()}
	 * @param productEvaluationDetailListener 回调监听器
	 */
	public void getProductEvaluationDetail(int productId, int page, ProductEvaluationDetailListener productEvaluationDetailListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			productEvaluationDetailListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("lastid", page);
			params.put("product_id", productId);
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_PRODUCT_EVALUATION_DETAIL, params, productEvaluationDetailListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_PRODUCT_EVALUATION_DETAIL + "?" + params);
		}
	}

	/**
	 * 上传商品的评价内容
	 * @param productId 商品id
	 * @param content 评价内容
	 * @param imageString 评价图片的地址, 取值为{@link #uploadEvaluationContent(int, String, String, UploadEvaluationContentListener)}该方法返回的结果
	 * @param uploadEvaluationContentListener 回调监听器
	 */
	public void uploadEvaluationContent(int productId, String content, String imageString, UploadEvaluationContentListener uploadEvaluationContentListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			uploadEvaluationContentListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("product_id", productId);
			params.put("contents", content);
			params.put("pics", imageString);
			params.put("scores", 0);//目前没作用
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().post(Constants.URL_PRODUCT_EVALUATION_UPLOAD_CONTENT, params, uploadEvaluationContentListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_PRODUCT_EVALUATION_UPLOAD_CONTENT + "?" + params);
		}
	}

	/**
	 * 上传商品的评价图片
	 * @param productId 商品id
	 * @param imagePath 本地图片地址
	 * @param uploadEvaluationPictureListener 回调监听器
	 */
	public void uploadEvaluationPicture(int productId, String imagePath, UploadEvaluationPictureListener uploadEvaluationPictureListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			uploadEvaluationPictureListener.onNoNetWork();

		}else {
			try {
				RequestParams params = new RequestParams();
				params.put("product_id", productId);
				params.put("image", new File(imagePath));
				SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
				SingtonAsyncHttpClient.getInstance().post(Constants.URL_PRODUCT_EVALUATION_UPLOAD_PICTURE, 
						params, uploadEvaluationPictureListener);
				CrashHandler.d(TAG, "url = " + Constants.URL_PRODUCT_EVALUATION_UPLOAD_PICTURE + "?" + params);
			} catch (FileNotFoundException e) {
				CrashHandler.e(e);
				uploadEvaluationPictureListener.onResponseFailed();
			}
		}
	}

	/**
	 * 上传申请退换货图片
	 * @param imagePath 本地图片地址
	 * @param uploadApplyExchangePictureListener 回调监听器
	 */
	public void uploadApplyExchangePicture(String imagePath, UploadApplyExchangePictureListener uploadApplyExchangePictureListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			uploadApplyExchangePictureListener.onNoNetWork();

		}else {
			try {
				RequestParams params = new RequestParams();
				params.put("image", new File(imagePath));
				SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
				SingtonAsyncHttpClient.getInstance().post(Constants.URL_PRODUCT_APPLY_EXCHANGE_UPLOAD_PICTURE, 
						params, uploadApplyExchangePictureListener);
				CrashHandler.d(TAG, "url = " + Constants.URL_PRODUCT_APPLY_EXCHANGE_UPLOAD_PICTURE + "?" + params);

			} catch (FileNotFoundException e) {
				CrashHandler.e(e);
				uploadApplyExchangePictureListener.onResponseFailed();
			}
		}
	}

	/**
	 * 提交退换货申请
	 * @param orderCode 订单号
	 * @param exchageType 退换货类型
	 * @param productIds 退换货的商品
	 * @param reason 退换货原因
	 * @param imageString 评价图片的地址, 取值为{@link #uploadApplyExchangePicture(String, UploadApplyExchangePictureListener)}该方法返回的结果
	 * @param submitApplyExchangeListener 回调监听器
	 */
	public void submitApplyExchange(String orderCode, int exchageType, String productIds, 
			String reason, String imageString, SubmitApplyExchangeListener submitApplyExchangeListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			submitApplyExchangeListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("sn", orderCode);
			params.put("returnsfor", exchageType);
			params.put("productIDs", productIds);
			params.put("returnsReason", reason);
			params.put("pics", imageString);
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().post(Constants.URL_PRODUCT_APPLY_EXCHANGE, params,
					submitApplyExchangeListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_PRODUCT_APPLY_EXCHANGE
					+ "?" + params);

		}
	}

	/**
	 * 获取订单确认数据
	 * @param orderForm 订单中的商品数据; 如: [{"act_id":1,"shop_id":1,"product_id":3,"num":5,"price":45.8}]
	 * @param orderConfirmationListener 回调监听器
	 */
	public void getOrderConfirmation(String orderForm, OrderConfirmationListener orderConfirmationListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			orderConfirmationListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("order_form", orderForm);
			params.put("cmid", IGZAddress.getInstance().getCurrentCommunityID());
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_ORDER_CONFIRMATION, params, orderConfirmationListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_ORDER_CONFIRMATION + "?" + params);
		}
	}

	/**
	 * 获取订单详情数据
	 * @param orderCoad 订单号
	 * @param orderDetailListener 回调监听器
	 */
	public void getOrderDetail(String orderCode, OrderDetailListener orderDetailListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			orderDetailListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("sn", orderCode);
			params.put("cmid", IGZAddress.getInstance().getCurrentCommunityID());
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_ORDER_DETAIL, params, orderDetailListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_ORDER_DETAIL + "?" + params);
		}
	}

	/**
	 * 获取订单列表数据
	 * @param page 页面; 取值为{@link OrdersInfo#getOrderId()}
	 * @param orderType 订单类型; 全部订单、待支付订单...等
	 * @param getOrdersListener 回调监听器
	 */
	public void getOrders(int page, String orderType, GetOrdersListener getOrdersListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			getOrdersListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("lastid", page);
			params.put("ostatus", orderType);
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_ORDERS, params, getOrdersListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_GET_ORDERS + "?" + params);
		}
	}

	/**
	 * 提交订单
	 * @param submitOrderInfo 订单数据
	 * @param submitOrderListener 回调监听器
	 */
	public void submitOrder(SubmitOrderInfo submitOrderInfo, SubmitOrderListener submitOrderListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			submitOrderListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("cmid", IGZAddress.getInstance().getCurrentCommunityID());
			params.put("money", String.valueOf(submitOrderInfo.getMoney()));
			params.put("usedGold", String.valueOf(submitOrderInfo.getGoldCount()));
			params.put("payType", submitOrderInfo.getPayType());
			params.put("nation", submitOrderInfo.getNation());
			params.put("province", submitOrderInfo.getProvince());
			params.put("city", submitOrderInfo.getCity());
			params.put("district", submitOrderInfo.getDistrict());
			params.put("address", submitOrderInfo.getAddress());
			params.put("postcode", submitOrderInfo.getPostcode());
			params.put("phone", submitOrderInfo.getPhone());
			params.put("receiver", submitOrderInfo.getReceiver());
			params.put("products", new String(submitOrderInfo.getProducts()));
			params.put("express_habit", submitOrderInfo.getDelivery());
			params.put("notes", (String) submitOrderInfo.getRemark());
			params.put("express_fee", submitOrderInfo.getPostage());
			params.put("cpucbt", submitOrderInfo.getDeliveryWay());
			params.put("houseid", submitOrderInfo.getHouseid());
			if(submitOrderInfo.getInvoice() == 1) {
				params.put("invoice", submitOrderInfo.getInvoice());
				params.put("invoice_name", submitOrderInfo.getInvoiceHead());
				params.put("invoice_type", submitOrderInfo.getInvoiceTypeName());
			}else {
				params.put("invoice", submitOrderInfo.getInvoice());
			}
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().post(Constants.URL_ORDER_CREATE, params, submitOrderListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_ORDER_CREATE + "?" + params);
		}
	}

	/**
	 * 取消一个订单
	 * @param orderCode 订单号
	 * @param cancelOrderListener 回调监听器
	 */
	public void cancelOrder(String orderCode, CancelOrderListener cancelOrderListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			cancelOrderListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("sn", orderCode);
			params.put("cmid", IGZAddress.getInstance().getCurrentCommunityID());
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_CANCEL_ORDER, params, cancelOrderListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_CANCEL_ORDER + "?" + params);
		}
	}

	/**
	 * 删除一个订单
	 * @param orderCode 订单号
	 * @param deleteOrderListener 回调监听器
	 */
	public void deleteOrder(String orderCode, DeleteOrderListener deleteOrderListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			deleteOrderListener.onNoNetWork();

		}else {
			deleteOrderListener.setOrderCode(orderCode);//绑定订单号
			RequestParams params = new RequestParams();
			params.put("sn", orderCode);
			params.put("packageSign", "packageSign");
			params.put("display", "1");
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().post(Constants.URL_EDIT_ORDER, params, deleteOrderListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_EDIT_ORDER + "?" + params);
		}
	}

	/**
	 * 获取参与页数据
	 * @param communityId 小区id
	 * @param page 页码; 取值为{@link ParticipationInfo#getParticationId()}
	 * @param getParticipationListener 回调监听器
	 */
	public void getParticipation(int communityId, int page, GetParticipationListener getParticipationListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			getParticipationListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("id", page);
			params.put("cmid", communityId);
			if(getParticipationListener.getRequestCode() == RequestCode.SCROLL) {
				params.put("flag", 1);
			}
			params.put("limit", "10");
			//			params.put("packname", mContext.getPackageName());
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_PARTICIPATION, params, getParticipationListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_PARTICIPATION + "?" + params);
		}
	}

	/**
	 * 获取发票类型
	 * @param getInvoiceTypeListener 回调监听器
	 */
	public void getInvoiceType(GetInvoiceTypeListener getInvoiceTypeListener) {	
		if(!Tools.hasInternetConnection(mContext)) {
			getInvoiceTypeListener.onNoNetWork();

		}else {
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_INVOICE_TYPE, getInvoiceTypeListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_GET_INVOICE_TYPE);
		}
	}

	/**
	 * 确认收货
	 * @param orderCode 订单号
	 * @param confirmTheGoodsListener 回调监听器
	 */
	public void confirmTheGoods(String orderCode, ConfirmTheGoodsListener confirmTheGoodsListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			confirmTheGoodsListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("sn", orderCode);
			params.put("packageSign", "packageSign");
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().post(Constants.URL_EDIT_ORDER, params, confirmTheGoodsListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_EDIT_ORDER + "?" + params);
		}
	}

	/**
	 * Avoid leaks by using a non-anonymous handler class.
	 */
	private static class ISmartMallHandler extends Handler {

		public ISmartMallHandler(Looper looper) {
			super(looper);
		}

		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			MessageData msgData;
			switch (msg.what) {
			case Constants.MSG_LOAD_BRANDED_PRODUCTS_FINISH:
				CrashHandler.d(TAG, "加载品牌商品列表缓存完成");
				msgData = (MessageData) msg.obj;
				List<AdvertInfo> advertInfos = (List<AdvertInfo>) msgData.value.get(0);
				List<BrandedProductsInfo> brandedProductsInfos =  (List<BrandedProductsInfo>) msgData.value.get(1);
				((BrandedProductsListener) msgData.listener).onResponseSuccess(
						advertInfos, brandedProductsInfos);
				break;


			default:
				break;
			}
		}
	}

	private static class MessageData { 
		BaseListener listener;
		List<Object> value;
	}

	/**
	 * 获取维修项目数据
	 */
	public void getMaintenacesProject(String mallcomid,String address,MaintenancesProjectListener getmainMaintenancesProjectListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			getmainMaintenancesProjectListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("mall_com_id", mallcomid);
			params.put("address", address);
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.UR_GET_EQUIMENT_PROJECT_ITEM, params, getmainMaintenancesProjectListener);
			CrashHandler.d(TAG, "url = " + Constants.UR_GET_EQUIMENT_PROJECT_ITEM + "?" + params);
		}
	}
	/**
	 * 获取维修列表
	 */
	public void getMaintenacesList(String mallcomid,int page,String addcode,String uid,final GetMaintenaceListListener getmaininfoListener){
		if(!Tools.hasInternetConnection(mContext)) {
			getmaininfoListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("add_code",addcode);
			params.put("page", page);
			params.put("mall_com_id", mallcomid);
			params.put("uid", uid);
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_EQUIMENT_LIST, params, getmaininfoListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_GET_EQUIMENT_LIST + "?" + params);
		}
	}
	/**
	 * 新增维修申报
	 */
	public void addMaintenaces(String mallcomid,String uid,MaintenacesInfos infos,String img1,String img2,String img3,String img4,int option_value,int com_id,int root_id,SubmitMaintenacesListener submitmainlistener){
		if(!Tools.hasInternetConnection(mContext)) {
			submitmainlistener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("apply_type", infos.getApplytype());
			params.put("apply_name", infos.getApplyname());
			params.put("telephone", infos.getTelephone());
			params.put("appointment_at", infos.getAppointmentat());
			params.put("appointment_at_end", infos.getAppointmentatend());
			params.put("address", infos.getAddress());
			params.put("repair_item", infos.getRepairitem());
			params.put("info", infos.getInfo());
			params.put("add_code", infos.getAddcode());
			params.put("map", "yes");
			params.put("mall_com_id", mallcomid);
			params.put("image_1", img1);
			params.put("image_2", img2);
			params.put("image_3", img3);
			params.put("image_4", img4);
			params.put("option_value", option_value+"");
			params.put("com_id", com_id+"");
			params.put("root_id ", root_id+"");
			params.put("uid", uid);
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().post(Constants.URL_ADD_EQUIMEMT, params, submitmainlistener);
			CrashHandler.d(TAG, "url = " + Constants.URL_ADD_EQUIMEMT + "?" + params);
		}
	}
	/**
	 * 获取当前登录用户的信息
	 */
	public void getMyAddress(MyAddressListener myAddressListener){
		if(!Tools.hasInternetConnection(mContext)) {
			myAddressListener.onNoNetWork();

		}else {
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_MYDEFAULTADDRESS, myAddressListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_GET_MYDEFAULTADDRESS);
		}
	}
	/**
	 * 确认完成维修
	 */
	public void updateRepair(String code,EquimentConfirmListener listener){
		if(!Tools.hasInternetConnection(mContext)) {
			listener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("code",code);
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().post(Constants.URL_TURE_EQUIMENT,params,listener);
			CrashHandler.d(TAG, "url = " + Constants.URL_TURE_EQUIMENT);
		}
	}

	/**
	 * 查询商品分类一级菜单
	 */
	public void getAssortFirstList(int cmid,final GetAssortFirstlevelListener getassortlevelListener){
		if (!Tools.hasInternetConnection(mContext)) {
			getassortlevelListener.onNoNetWork();
			return;
		}

		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_FIRST_PRODUCTS + "?cmid=" + cmid, getassortlevelListener);
	}

	/**
	 * 查询商品分类
	 */
	public void getAssortProductsList(int kindid,int page,final GetAssortProductslListener getAssortProductsListener){
		if (!Tools.hasInternetConnection(mContext)) {
			getAssortProductsListener.onNoNetWork();
			return;
		}

		RequestParams params = new RequestParams();
		params.put("kind_id",kindid);
		params.put("page",page);
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_ASSORT_PRODUCTS,params,getAssortProductsListener);
		CrashHandler.d(TAG, "url = " + Constants.URL_GET_ASSORT_PRODUCTS);
	}

	/**
	 * 查询闪屏广告
	 */
	public void getAdvertise(AdvertiseListener getadverListener){
		if (!Tools.hasInternetConnection(mContext)) {
			getadverListener.onNoNetWork();
			return;
		}

		RequestParams params = new RequestParams();
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_ADVERTISE,params,getadverListener);
		CrashHandler.d(TAG, "url = " + Constants.URL_GET_ADVERTISE);
	}
	/**
	 * 查询合作联盟商家
	 * @param getadverListener
	 */
	public void get_join_pic(ShopsIntroduceListener shopListener){
		if (!Tools.hasInternetConnection(mContext)) {
			shopListener.onNoNetWork();
			return;
		}

		RequestParams params = new RequestParams();
		params.put("cmid", IGZAddress.getInstance().getCurrentCommunityID());
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_JSON_SHOP,params,shopListener);
		CrashHandler.d(TAG, "url = " + Constants.URL_GET_JSON_SHOP);
	}
	/**
	 * 上传报修图片
	 * @param productId 商品id
	 * @param imagePath 本地图片地址
	 * @param uploadEvaluationPictureListener 回调监听器
	 */
	public void uploadEquimentPicture(String file, String file_name, UploadEquimentPictureListener uploadEquimentPictureListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			uploadEquimentPictureListener.onNoNetWork();

		}else {
			RequestParams params = new RequestParams();
			params.put("file", file);
			params.put("file_name", file_name);
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().post(Constants.URL_POST_EQUIMENTIMG, 
					params, uploadEquimentPictureListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_POST_EQUIMENTIMG + "?" + params);
		}
	}
}
